/*
 * These files are distributed under the terms of the GNU GPLv2, the GNU General
 * Public version 2. See LICENSE.txt
 */
package fileCommodore.plugin.copy;

import java.awt.event.*;
import java.util.*;

import org.apache.commons.vfs.*;

import fileCommodore.*;
import fileCommodore.model.*;
import fileCommodore.plugin.*;

/**
 * @author nado18@gmail.com, on Aug 3, 2007
 */
public class CopyPlugin extends Plugin
{
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see fileCommodore.plugin.Plugin#activeSelectionChanged()
	 */
	@Override
	public void activeSelectionChanged()
	{
		// TODO Auto-generated method stub
		
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see fileCommodore.plugin.Plugin#handle(java.awt.event.KeyEvent)
	 */
	@Override
	public void handle( KeyEvent ev )
	{
		if (KeyEvent.VK_F5 == ev.getKeyCode())
		{
			Vector<FileRowItem> tagged = getActiveSelection().getTagged();
			FileObject adjacentDir =
					getInactiveSelection().getCurrent().getFileObject();
			
			if (tagged.isEmpty())
			{
				FileObject selectedFile =
						getActiveSelection().getSelected().getFileObject();
				try
				{
					Main.mergeCopy( selectedFile, adjacentDir );
				}
				catch (FileSystemException e)
				{
					showError(
						"Copy Error",
						"There was an error copying the selected file.",
						e );
				}
			}
			else
			{
				StringBuffer sb = new StringBuffer();
				
				for (FileRowItem item : getActiveSelection().getTagged())
				{
					try
					{
						Main.mergeCopy( item.getFileObject(), adjacentDir );
					}
					catch (FileSystemException e)
					{
						sb.append( item.getFileName() ).append( "\r\n" );
						
						e.printStackTrace();
					}
				}
				
				if (sb.length() > 0)
				{
					sb.insert( 0, "There were errors copying these files:\r\n" );
					String msg = sb.toString();
					showError( "Copy Error", msg );
				}
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see fileCommodore.plugin.Plugin#inactiveSelectionChanged()
	 */
	@Override
	public void inactiveSelectionChanged()
	{
		// TODO Auto-generated method stub
		
	}
	
}
